//
//  MessageCenterCell.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/6/23.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "MessageCenterCell.h"
#import "UIView+Badge.h"

@interface MessageCenterCell ()

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *messageLab;
@property (nonatomic, strong) UILabel *messageTimeLab;

@end

@implementation MessageCenterCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews
{
    self.icon = [[UIImageView alloc] init];
    self.icon.image = [UIImage imageNamed:@"message_ybxx"];
    self.icon.badgeOriginX = @36;
    self.icon.badgeOriginY = @(-5);
    [self addSubview:self.icon];
    
    self.nameLab = [[UILabel alloc] init];
    self.nameLab.textColor = kColor333333;
    self.nameLab.font = [UIFont systemFontOfSize:16];
    [self addSubview:self.nameLab];
    
    self.messageLab = [[UILabel alloc] init];
    self.messageLab.textColor = kColor666666;
    self.messageLab.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.messageLab];
    
    self.messageTimeLab = [[UILabel alloc] init];
    self.messageTimeLab.textColor = kColor666666;
    self.messageTimeLab.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.messageTimeLab];
    
    [self.icon setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(16);
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icon);
        make.left.equalTo(self.icon.mas_right).offset(10);
        make.right.lessThanOrEqualTo(self.messageTimeLab.mas_left).offset(-16);
    }];
    
    [self.messageTimeLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.messageTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icon);
        make.right.equalTo(self).offset(-16);
    }];
    
    [self.messageLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.icon);
        make.left.equalTo(self.icon.mas_right).offset(10);
        make.right.lessThanOrEqualTo(self).offset(-16);
    }];
}

- (void)setEntity:(MessageCenterEntity *)entity
{
    _entity = entity;
    self.nameLab.text = entity.orgName;
    self.messageLab.text = String(entity.content);
    self.messageTimeLab.text = String(entity.createTime);
    self.icon.badgeValue = [NSString stringWithFormat:@"%@",entity.unReadCnt];
}

@end
